package com.kot.backend.service.impl.user.myspace;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kot.backend.mapper.BotMapper;
import com.kot.backend.mapper.PostMapper;
import com.kot.backend.mapper.UserMapper;
import com.kot.backend.pojo.Bot;
import com.kot.backend.pojo.Post;
import com.kot.backend.pojo.User;
import com.kot.backend.service.impl.utils.UserDetailsImpl;
import com.kot.backend.service.user.myspace.UserlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserlistServiceImpl implements UserlistService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PostMapper postMapper;


    @Override
    public List<User> getuserlist() {
        List<User> list = new ArrayList<>();
        list = userMapper.selectList(null);
        System.out.println(list);
        return list;
    }

    @Override
    public Map<String, String> follow(Integer targetId) {
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();
        Map<String, String> map = new HashMap<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", targetId);
        User target = userMapper.selectOne(queryWrapper);//获取目标对象
        String ids = target.getAttentionids();//目标对象的关注者id
        String[] ans_ids = new String[]{};
        StringJoiner sj = new StringJoiner("#", "", "");
        boolean flag = false;
        if(ids!=null){
            ans_ids = ids.split("#");
            System.out.println(Arrays.toString(ans_ids));
            for (int i = 0; i < ans_ids.length; i++) {
                if(ans_ids[i].length()<1)continue;
                if (Integer.parseInt(ans_ids[i]) != user.getId()) {
                    sj.add( ans_ids[i]);
                } else {
                    flag = true;//表示已经关注了，需要删除，进行取关操作
                }
            }
        }
        if(!flag){
            sj.add(user.getId().toString());
            target.setFollowercount(target.getFollowercount()+1);
        }else{
            target.setFollowercount(target.getFollowercount()-1);
        }
        target.setAttentionids(sj.toString());
        userMapper.updateById(target);
        System.out.println("target="+target);
        map.put("result","success");
        return map;
    }
    @Override
    public User getuserbyid(Integer userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);

        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Map<String, String> postApost(String content) {
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();

        Post post = new Post(null, content,user.getId(),new Date());

        postMapper.insert(post);
        Map<String,String> map = new HashMap<>();
        map.put("result","success");
        return map;
    }

    @Override
    public List<Post> getpostbyid(Integer userId) {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("posttime");
        return postMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, String> deleteApost(Integer postId) {
        postMapper.deleteById(postId);
        Map<String,String> map = new HashMap<>();
        map.put("result","success");
        return map;
    }

    @Override
    public List<Post> getpostall() {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("posttime");
        return  postMapper.selectList(queryWrapper);
    }
}
